*Inaction rates at larger establishments/employment-weighted, by size, and by positive hires
cd /OEUS/users/FEDSusers/JOLTS/
set more off

use ./data/JOLTS_ntilde, clear


foreach i of numlist 1/24{
	gen inactalt_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace inactalt_`i' = 1 if emp == ntilde`i'  & inactalt_`i' == 0
	
	gen inactaltposh_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 & hires`i'>0 & hires`i'!=.
	replace inactaltposh_`i' = 1 if emp == ntilde`i'  & inactaltposh_`i' == 0 
	
	gen inactaltg20_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i'>20 
	replace inactaltg20_`i' = 1 if emp == ntilde`i'  & inactaltg20_`i' == 0 & emp>0 & ntilde`i'>20 
}

*Define quarterly inaction with a window as maximum of emp and percentage
*max(1,1%)

foreach i of numlist 1/24{
	gen inactalt11_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace inactalt11_`i' = 1 if emp >= min(floor(.99*ntilde`i'),ntilde`i'-1) & emp<=max(ceil(1.01*ntilde`i'),ntilde`i'+1) & inactalt11_`i' == 0
	
	gen inactalt11posh_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 & hires`i'>0 & hires`i'!=.	
	replace inactalt11posh_`i'= 1 if emp >= min(floor(.99*ntilde`i'),ntilde`i'-1) & emp<=max(ceil(1.01*ntilde`i'),ntilde`i'+1) & inactalt11posh_`i' == 0  
	
	gen inactalt11_g20_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >20 
	replace inactalt11_g20_`i' = 1 if emp >= min(floor(.99*ntilde`i'),ntilde`i'-1) & emp<=max(ceil(1.01*ntilde`i'),ntilde`i'+1) & inactalt11_g20_`i' == 0 & emp>0 & ntilde`i'>20 
	
}

*max(2,2%)
foreach i of numlist 1/24{
	gen inactalt22_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace inactalt22_`i' = 1 if emp >= min(floor(.98*ntilde`i'),ntilde`i'-2) & emp<=max(ceil(1.02*ntilde`i'),ntilde`i'+2) & inactalt22_`i' == 0 
	
	gen inactalt22posh_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0  & hires`i'>0 & hires`i'!=.	
	replace inactalt22posh_`i' = 1 if emp >= min(floor(.98*ntilde`i'),ntilde`i'-2) & emp<=max(ceil(1.02*ntilde`i'),ntilde`i'+2) & inactalt22posh_`i' == 0 
	
	gen inactalt22_g20_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >20 
	replace inactalt22_g20_`i' = 1 if emp >= min(floor(.98*ntilde`i'),ntilde`i'-2) & emp<=max(ceil(1.02*ntilde`i'),ntilde`i'+2) & inactalt22_g20_`i' == 0  & emp>0 & ntilde`i'>20 
}

cd /OEUS/users/FEDSusers/JOLTS/rnd2final/
*********Estab Weight
preserve
collapse (mean) inactalt_* inactalt11_* inactalt22_* (count) N = weight_f [aw=weight_f]
gen month=0
reshape long inactalt_ inactalt11_ inactalt22_, i(month)
drop month
rename _j month
save inactdecay_estab_rnd2final, replace
outsheet using inactdecay_estab_rnd2final.csv, c replace
restore

********Emp Weight
preserve
gen weight2 = weight_f*emp
collapse (mean) inactalt_* inactalt11_* inactalt22_* (count) N = weight_f [aw=weight2]
gen month=0
reshape long inactalt_ inactalt11_ inactalt22_, i(month)
drop month
rename _j month
save inactdecay_emp_rnd2final, replace
outsheet using inactdecay_emp_rnd2final.csv, c replace
restore
*********Cond. on emp>20, estab weight
preserve
collapse (mean) inactaltg20_* inactalt11_g20_* inactalt22_g20_* (count) N = weight_f [aw=weight_f]
gen month=0
reshape long inactaltg20_ inactalt11_g20_ inactalt22_g20_, i(month)
drop month
rename _j month
save inactdecay_g20_estab_rnd2final, replace
outsheet using inactdecay_g20_estab_rnd2final.csv, c replace 
restore

*********Cond. on emp>20, emp weight
preserve
gen weight2 = weight_f*emp
collapse (mean) inactaltg20_* inactalt11_g20_* inactalt22_g20_* (count) N = weight_f  [aw=weight2]
gen month=0
reshape long inactaltg20_ inactalt11_g20_ inactalt22_g20_, i(month)
drop month
rename _j month
save inactdecay_g20_emp_rnd2final, replace
outsheet using inactdecay_g20_emp_rnd2final.csv, c replace
restore

*********Cond. on pos hires, estab weight
preserve
collapse (mean) inactaltposh_* inactalt11posh_* inactalt22posh_* (count) N = weight_f [aw=weight_f]
gen month=0
reshape long inactaltposh_ inactalt11posh_ inactalt22posh_, i(month)
drop month
rename _j month
save inactdecay_posh_estab_rnd2final, replace
outsheet using inactdecay_posh_estab_rnd2final.csv, c replace
restore

*********Cond. on pos hires, emp weight
preserve
gen weight2 = weight_f*emp
collapse (mean) inactaltposh_* inactalt11posh_* inactalt22posh_* (count) N = weight_f [aw=weight2]
gen month=0
reshape long inactaltposh_ inactalt11posh_ inactalt22posh_, i(month)
drop month
rename _j month
save inactdecay_posh_emp_rnd2final, replace
outsheet using inactdecay_posh_emp_rnd2final.csv, c replace
